package org.swings.base;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Properties;

import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

import org.apache.log4j.Logger;
import org.swings.base.contants.ApplicationBaseInterfaceConstants;
import org.swings.base.status.view.StatusPanel;
import org.swings.tools.ToolsBarSwings;

/**
 * <p>
 * <b>Overview:</b>
 * <p>
 * 
 * 
 * <pre>
 * Creation date: Feb 16, 2015
 * @author Amit Kshirsagar
 * @version 1.0
 * @since
 * 
 * <p><b>Modification History:</b><p>
 * 
 * 
 * </pre>
 */

public abstract class BaseMasterFrame extends JFrame implements
		ApplicationBaseInterfaceConstants, ActionListener, MouseListener,
		FocusListener {

	private static Logger log = Logger.getLogger(BaseMasterFrame.class
			.getName());

	private static SplashScreen _splashScreen = null;
	private static Properties _properties = null;

	private JTabbedPane tabbedPanel = null;
	private HashMap<String, JPanel> tabbedPanelHash = new HashMap<String, JPanel>();

	public void disposeSplash() {
		_splashScreen.dispose();
	}

	public abstract void createComponents();

	public void initilizeSplash(String iImgName) {
		if (iImgName == null || iImgName.equals("")) {
			iImgName = System.getProperty("JSPLASH_SCREEN");
		}
		log.info("Pop SplashScreen (System Property: JSPLASH_SCREEN): "
				+ iImgName);
		_splashScreen = new SplashScreen(this, new ImageIcon(iImgName));
		_splashScreen.setVisible(true);
		_splashScreen.setAlwaysOnTop(true);
		slowDownASecond();
	}

	public void buildMasterFrame() {
		setDefaultApplicationSettings();
		prepareView();
		loadStatusBarPanel();
		populateTabbedPanel();
		loadToolBarPanel();
	}

	public void readProperties(String applicationPropertiesFileName) {
		_properties = new Properties();

		if (BaseUtility.isEmpty(applicationPropertiesFileName)) {
			log.debug("Using Default properties File Name: JCONFIGURATION_FILE");
			applicationPropertiesFileName = "JCONFIGURATION_FILE";
		}
		try {
			_properties
					.load(new FileInputStream(applicationPropertiesFileName));
		} catch (FileNotFoundException e) {
			log.error("Error reading File.." + e.getMessage(), e);
		} catch (IOException e) {
			log.error("Error reading File..." + e.getMessage(), e);
		}
		log.error("Read Some Properties...");
	}

	/**
	 * 
	 */
	public void loadToolBarPanel() {
		ToolsBarSwings toolsBar = new ToolsBarSwings(
				this.getPropertyValue(JCOMPONENT_CONFIG), this);
		_maintPanel.add(toolsBar, BorderLayout.NORTH);
	}

	public void loadStatusBarPanel() {
		StatusPanel statusPanel = StatusPanel.loadPanel(this,
				BorderLayout.SOUTH);
		setStatusPanel(statusPanel);
	}

	public JPanel controlPanel;
	public JPanel _maintPanel = null;

	public void prepareView() {
		/*
		 * Set Frame Specific attributes here....
		 */
		this.setSize(new Dimension(1000, 750));

		_maintPanel = (JPanel) this.getContentPane();
		_maintPanel.setLayout(new BorderLayout());

		this.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent windowEvent) {
				System.exit(0);
			}
		});

	}

	/**
	 * 
	 */
	private void setDefaultApplicationSettings() {
		try {
			for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					UIManager.setLookAndFeel(info.getClassName());
					UIManager.put("nimbusOrange", new Color(40, 225, 40));
					UIManager.put("Table.background", Color.WHITE);
					UIManager.put("Table.alternateRowColor", Color.BLUE);
					UIManager.put("nimbusSelectionBackground", new Color(220,
							220, 220));
					UIManager.put("List[Selected].textBackground", new Color(
							220, 220, 220));
					break;
				}
			}
		} catch (Exception e) {
			// If Nimbus is not available, you can set the GUI to another look
			// and feel.
		}

		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent windowEvent) {
				System.exit(0);
			}
		});

		this.setSize(new Dimension(1000, 750));

		// _applicationPanel = (JPanel) this.getContentPane();
		this.setLayout(new BorderLayout());
	}

	public void populateTabbedPanel() {
		tabbedPanel = new JTabbedPane();
		_maintPanel.add(tabbedPanel, BorderLayout.CENTER);
	}

	public void addTabbedPanel(BasePanel tab) {
		if (!tabbedPanelHash.containsKey(tab.getName())) {
			tabbedPanel.addTab(tab.getName(), tab);
			tab.setStatusPanel(statusPanel);
			tabbedPanelHash.put(tab.getName(), tab);
		}
	}

	public StatusPanel statusPanel = null;

	public void setStatusPanel(StatusPanel statusPanel) {
		this.statusPanel = statusPanel;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
	 */
	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
	 */
	@Override
	public void mouseEntered(MouseEvent arg0) {
		JComponent button = (JComponent) arg0.getSource();
		button.setBackground(Color.BLUE);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
	 */
	@Override
	public void mouseExited(MouseEvent arg0) {
		JComponent button = (JComponent) arg0.getSource();
		button.setBackground(new Color(214, 217, 223));

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
	 */
	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
	 */
	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		final String command = e.getActionCommand();
		Runnable actionRunner = new Runnable() {

			@Override
			public void run() {
				if (command.equalsIgnoreCase("OK")) {
					executeOK();
				} else if (command.equalsIgnoreCase("SUBMIT")) {
					executeSubmit();
				} else if (command.equalsIgnoreCase("CANCEL")) {
					executeCancel();
				} else if (command.equalsIgnoreCase("RESET")) {
					executeReset();
				} else {
					executeButtonAction(command);
				}
			}
		};

		Thread actionThread = new Thread(actionRunner, e.getSource().toString());
		actionThread.start();
	}

	/**
	 * 
	 */
	public abstract void executeOK();

	/**
	 * 
	 */
	public abstract void executeSubmit();

	/**
	 * 
	 */
	public abstract void executeCancel();

	/**
	 * 
	 */
	public abstract void executeReset();

	public abstract void executeButtonAction(String actionName);

	private void slowDownASecond() {

		try {
			Thread.sleep(600);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public String getPropertyValue(String key) {
		return (String) _properties.get(key);
	}

}
